package com.zygh.lz.service.impl;

import com.zygh.lz.entity.Role;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.security.CustomUserDetails;
import com.zygh.lz.service.RoleService;
import com.zygh.lz.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class JwtUserDetailsService implements UserDetailsService {

    @Resource
    private StaffService userAccountService;


    @Resource
    private RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String value) throws UsernameNotFoundException {

        Staff userAccount = userAccountService.selectStaffByNum(value);
        if (userAccount == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", value));
        }
        List<Role> allRole = roleService.findAllRole(value);

        return new CustomUserDetails(userAccount.getStaffNum(), userAccount.getStaffPassword(), allRole);
    }
}
